package com.itheima.Service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.CheckItemDao;
import com.itheima.Service.CheckItemService;
import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;
    @Override
    public void add(CheckItem checkItem) {
        if (checkItemDao.selectBYName(checkItem.getName())!=null) {
            log.info("检查项重复");
            throw new  RuntimeException  (MessageConstant.ADD_CHECKGROUP_FAIL);
        }else {
            checkItemDao.add(checkItem);
        }
    }

    @Override
    public void delete(Integer id) {

        checkItemDao.delete(id);
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
       return  checkItemDao.findCheckItemIdsByCheckGroupId(id);

    }

    @Override
    public void edit(CheckItem checkItem) {
        if (checkItemDao.selectBYID(checkItem.getId())==null) {
            log.info("无此检查项");
            throw new  RuntimeException  (MessageConstant.EDIT_CHECKGROUP_FAIL);
        }else {
            checkItemDao.edit(checkItem);
        }

    }
    @Override
    public PageResult findPage(QueryPageBean page) {
        PageHelper.startPage(page.getCurrentPage(),page.getPageSize());
        List<CheckItem>checkItems=checkItemDao.findPage(page);
        Page<CheckItem>checkItems1=(Page<CheckItem>)checkItems;
        PageResult pageResult =new PageResult(checkItems1.getTotal(),checkItems);
        return pageResult;
    }

    @Override
    public List<CheckItem> findAll() {

        return checkItemDao.findAll();
    }

    @Override
    public CheckItem findById(Integer id) {
        return checkItemDao.findById(id);
    }
}
